//
//  TableFormCell.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/3.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import YogaKit
import CooperSwift

//extension UITableViewCell: Bind

class TableFormCell: UITableViewCell {
    
    var bgView: UIView?
    var leftViews: [UIView]?
    var rightViews: [UIView]?
    var centerViews: [UIView]?
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        layoutViews()
    }
    
    
    override func bindTo(_ info: Any?) {
        guard let lefts = leftViews else { return }
        guard let info = info as? TableFormCellModel else {
            return
        }
        if let lb = lefts.first as? UILabel {
            lb.text = (info.model as! String)
            lb.configureLayout {
                $0.isEnabled = true
            }
            lb.yoga.applyLayout(preservingOrigin: true)
        }
        
//        bgView?.yoga.applyLayout(preservingOrigin: true)
        contentView.yoga.applyLayout(preservingOrigin: true)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


extension TableFormCell {
    
    func layoutViews() {
        let bgV = UIView.init().then {
            $0.backgroundColor = .random()
        }
        contentView.addSubview(bgV)
        bgView = bgV
        contentView.configureLayout {
            $0.isEnabled = true
            $0.flexDirection = .row
            $0.width = YGValue(Screen.width)
            $0.height = YGValue(self.contentView.height)
        }
        
        bgV.configureLayout {
            $0.isEnabled = true
            $0.flexDirection = .row
            $0.width = YGValue(Screen.width)
            $0.alignItems = .center
        }
        
        leftViews = arroundViews(700)
        leftViews!.forEach{ v in
            v.configureLayout {
                $0.isEnabled = true
            }
        }
        bgV.addSubviews(leftViews!)
        
        centerViews = centerV()
        centerViews!.forEach { v in
            v.configureLayout {
                $0.isEnabled = true
                $0.flexGrow = 1
            }
        }
        
        bgV.addSubviews(centerViews!)
        
        rightViews = arroundViews(900).reversed()
        rightViews!.forEach{ v in
            v.configureLayout {
                $0.isEnabled = true
            }
        }
        bgV.addSubviews(rightViews!)
        
        contentView.yoga.applyLayout(preservingOrigin: true)
        
    }
    
    func centerV() -> [UIView] {
        return [createTF(90000)]
//        return [createTF(90000), createLB(90001), createTV(90002), createBT(90003)]
    }
    
    func arroundViews(_ tag: Int) -> [UIView] {
        return [createLB(tag), createImgV(1000 + tag), createLB(tag + 1), createImgV(1001 + tag), createLB(tag + 2), createImgV(1002 + tag), createLB(tag + 4), createImgV(1004 + tag)]
    }
    
    
    func createTV(_ tag: Int) -> UITextView {
        let tv = UITextView.init().then {
            $0.tag = tag
        }
        return tv
    }
    
    func createBT(_ tag: Int) -> UIButton {
        let bt = UIButton.init().then {
            $0.tag = tag
        }
        return bt
    }
    
    func createTF(_ tag: Int) -> UITextField {
        let tf = UITextField.init().then {
            $0.tag = tag
        }
        return tf
    }
    
    
    func createImgV(_ tag: Int) -> UIImageView {
        let imgV = UIImageView.init().then {
            $0.tag = tag
        }
        return imgV
    }
    
    func createLB(_ tag: Int) -> UILabel {
        let lb = UILabel.init().then {
            $0.text = "xxx"
            $0.textColor = .darkText
            $0.tag = tag
        }
        return lb
    }
}

class TableFormCellModel: NSObject, TableCellConfigProtocol, BindKeyProtocol {

    var model: Any?
    
    required init(_ model: Any?) {
        self.model = model
    }
    
    func cellCls() -> UITableViewCell.Type {
        return TableFormCell.self
    }
    
    func cellHeight() -> CGFloat? {
        return 44
    }
    
    typealias TargetType = UIView
    
    func once(_ target: UIView, cell: UITableViewCell) {
        
    }
    
    func every(_ target: UIView, cell: UITableViewCell) {
        
    }
    
}
